package com.jdbc.studyReflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionInstance {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
        Person p = new Person();
        Class<?> classtype1 = p.getClass();
        Class<?> classtype2 = Class.forName("com.jdbc.studyReflection.Person");
        Class<?> classtype3 = Person.class;
        System.out.println(classtype1 == classtype2);
        System.out.println(classtype2 == classtype3);
        Field field1 = classtype1.getDeclaredField("weight");
        Field field2 = classtype1.getDeclaredField("name");
        Field field3 = classtype1.getDeclaredField("age");
        Field field4 = classtype1.getDeclaredField("height");
        System.out.println(field1);
        System.out.println(field2);
        System.out.println(field3);
        System.out.println(field4);
        System.out.println("-=-=-=-=-=-=-=-=-=-");
        Field[] fields = classtype1.getDeclaredFields();
        for(Field field:fields){
            System.out.println(field);
        }
        System.out.println("++++++++++++++++++++++");
        System.out.println(field1.isAccessible());
        field1.setAccessible(true);
        System.out.println(field1.get(p));
        field1.set(p,34.34);
        System.out.println(field1.get(p));

        Object mObj0 = classtype3.getDeclaredConstructor(String.class).newInstance("wuhu");
        System.out.println(mObj0);
        Object mObj1 = classtype3.getConstructor().newInstance();
        System.out.println(mObj1);
        System.out.println(classtype3.getDeclaredConstructor(double.class).isAccessible());
        Constructor doubleConstructor = classtype3.getDeclaredConstructor(double.class);
        doubleConstructor.setAccessible(true);
        System.out.println(doubleConstructor.isAccessible());
        Object mObj3 = doubleConstructor.newInstance(23.65);
        System.out.println(mObj3);

        System.out.println();
        Object mPerson = classtype3.newInstance();
        Method method1 = classtype3.getDeclaredMethod("setWeight", double.class);
        method1.invoke(mPerson,99.99);
        System.out.println();
        Method method2 = classtype3.getMethod("setName", String.class);
        method2.invoke(mPerson,"dasima");
    }
}
class Person{
    public String name = "tom";
    int age = 20;
    private double weight = 70.25;
    protected int height = 180;

    public Person(){}

    private Person(double weight){
        this.weight = weight;
    }
    Person(String name,int age,double weight,int height){
        this.name = name;
        this.age = age;
        this.weight = weight;
        this.height = height;
    }
    protected Person(String name){
        this.name = name;
    }

    public void setName(String name){
        this.name = name;
        System.out.println(this.name);
    }
    int getAge(){
        return this.age;
    }
    protected void setWeight(double weight){
        this.weight = weight;
        System.out.println(this.weight);
    }
    private int getHeight(){
        return this.height;
    }
}